<?php

namespace app\common\model\transit;

use app\api\controller\Pub;
use app\common\model\user\User;
use think\Model;

class TransitDriver extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'transitdriver';
    protected $pk    = 'TransitDriverId';

    /**
     * 创建企业信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_create($data, $type = 'tran')
    {
        $userlogininfo = Pub::getuserid();
        $userid        = $userlogininfo['msg'];

        if ($type == 'tran') {
            $tc   = Transitcompany::user_getinfo();
            $tcid = $tc['TransitCompanyId'];
        } else if ($type == 'mana') {
            $tcid = $data['id'];
        }
        //判断是否存在
        $sb = TransitDriver::where('UserName', $data['UserName'])->where('UserTel', $data['UserTel'])->where('TransitCompanyId',$tcid)->find();
        if (!empty($sb)) {
            return moerr('此司机已存在。');
        }

        $tsp                   = new TransitDriver();
        $tsp->UserName         = $data['UserName'];
        $tsp->UserTel          = $data['UserTel'];
        $tsp->UserCard         = $data['UserCard'];
        $tsp->TransitCompanyId = $tcid;
        $tsp->Img              = $data['Img'];
        $tsp->Note             = $data['Note'];

        $tsp->save();
        return $tsp->TransitDriverId;
    }

    /**
     * 用户修改企业信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_reinfo($id, $data, $type = 'tran')
    {
        $company = TransitDriver::get($id);
        if ($type == 'tran') {
            $sb = Transitcompany::get($company['TransitCompanyId']);
            if ($sb['UserId'] !== Pub::userid()) {
                return moerr('没有修改权限');
            }
        }
        $userinfo = User::getinfo();
        $nreinfo  = '';
        foreach ($data as $key => $value) {
            $sb = $company->getfiledname($key);
            if (empty($sb)) {
                continue;
            }
            if (!empty($value) && $company->$sb !== $value) {
                $nreinfo = $nreinfo . '字段【' . $company->getfiledname($key, 2) . '】由【' . $company->$sb . '】修改为【' . $value . '】;' . "\n";
                //输入来的参数名非字段名需要进行替换
                $company->$sb = $value;
            }
        }
        if (!empty($nreinfo)) {
            $company->save();
        }
    }

    /**
     * 获取订单列表
     * @param  [type] $data    [主数据]
     * @param  string $type    [查询类型/hawa/buss]
     * @param  [type] $is_page [是否分页显示]
     * @return [type]          [description]
     */
    public static function usergetlist($data, $type = 'tran', $is_page = true)
    {
        if ($type == 'tran') {
             //有企业id直接设置企业id为条件 没有则判断是否为管理者
            if(empty($data['tid'])){
                $company = Transitcompany::user_getinfo();
                $rdata    = TransitDriver::where('TransitCompanyId', $company['TransitCompanyId']);
            }else{
                $rdata = TransitDriver::where('TransitCompanyId',$data['tid']);
            }
        } else  if ($type == 'buss') {
            $rdata = TransitDriver::order('TransitCompanyId desc');
        } else {
            return moerr('缺少参数');
        }
        $option = come_model_option($data, $rdata, $is_page);

        $rdata = $option['rdata'];
        $data  = $option['data'];

        if (!empty($data['field'])) {
            $rdata = $rdata->field($visible);
        }else{
             //隐藏修改日志
            $rdata = $rdata->hidden(['OperationLog']);
        }
       
        $rdata = $rdata->select();
        if (empty($data['field']) && !empty($visible)) {
            $rdata = $rdata->visible($visible);
        }

        $ndata['list']      = $rdata->toArray();
        if($type=='buss'){
            $ndata['list'] = arr_id_formate($ndata['list'],'TransitCompanyId');
        }
        $ndata['statetext'] = Transitcompany::getStatusText();
        // $goodid = get_array_id($ndata,'GoodId','list','listinfo');
        if ($is_page) {
            $ndata['pageinfo'] = ['pageindex' => $data['pageindex'], 'pagemax' => $data['count']];
        }
        return $ndata;
    }

    public static function getfiledname($str, $type = 1)
    {
        $filename = ['UserName' => '姓名',
            'UserCard'              => '身份证',
            'UserTel'               => '联系方式',
            'Img'                   => '图片信息',
            'Note'                  => '备注信息'];
        if ($type == 1) {
            if (empty($filename[$str])) {
                return false;
            }
            return $str;
        } else {
            if (empty($filename[$str])) {
                return false;
            }
            return $filename[$str];
        }
    }

}
